package com.example.sonny.util.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.sonny.bean.BookItem;
import com.example.sonny.dutest.R;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

/**
 * Created by Sonny on 2015/8/26.
 */
public class BookGridAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        ArrayList<BookItem> books;

        public BookGridAdapter(Context context, ArrayList<BookItem> books) {
            this.inflater = LayoutInflater.from(context);
            this.books = books;
        }


    @Override
    public int getCount() {
        return books.size();
    }

    @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView==null) {
                holder=new ViewHolder();
                convertView=this.inflater.inflate(R.layout.book_grid_item, null);
                holder.iv=(ImageView) convertView.findViewById(R.id.book_grid_item_iv);
                holder.tv=(TextView) convertView.findViewById(R.id.book_grid_item_tv);
                convertView.setTag(holder);
            }
            else {
                holder=(ViewHolder) convertView.getTag();
            }
            ImageLoader imageLoader = ImageLoader.getInstance();
            imageLoader.displayImage(books.get(position).getImgUrl(),holder.iv);
            holder.tv.setText(books.get(position).getName());
            return convertView;
        }
        private class ViewHolder{
            ImageView iv;
            TextView tv;
        }

}
